<template>
  <div class="flex_label">
    <div class="label_div">
      <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="序号" align="center" prop="order" width="50" />
        <el-table-column label="资产编号" align="center" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.assetNum || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="设备编号" align="center" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.equipmentNum || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="设备名称" align="center" min-width="130" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.equipmentBaseVo && scope.row.equipmentBaseVo.equipmentName || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="规格型号" align="center" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.equipmentBaseVo && scope.row.equipmentBaseVo.model || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="位置" align="center" min-width="130" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.placeName || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="最后打印时间" align="center" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.lastPrintTime || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="创建部门" align="center" min-width="180" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.deptName || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.createBy || '-' }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
        <!-- <el-table-column label="操作" min-width="220" fixed="right" align="center" class-name="medium-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="primary" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.status === '0'" size="small" type="danger" @click="handleDelete(scope.row)">禁用
          </el-button>
          <el-button v-else size="small" type="success" @click="handleDelete(scope.row)">启用</el-button>
        </template>
      </el-table-column> -->
      </el-table>
      <!-- <div class="card-div"> -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <div class="label_div">
      <el-button size="medium" icon="el-icon-view" :loading="loadingbtn" @click="PreviewView">预览</el-button>
      <el-button size="medium" type="primary" icon="el-icon-download" @click="handlePrint" plain>打印</el-button>
      <div id="printDiv">
        <el-card class="ImgCode_flex" style="clear:both;page-break-inside:avoid" v-for="(item, index) in browseList"
          :key="index">
          <div class="div_flex" style="flex: 1;float: left;">
            <img :src="item.img" style="width:200px;height:200px;margin:25px 0px;">
          </div>
          <div class="div_flex"
            style="flex: 2;display: flex;flex-direction: column;justify-content: space-evenly;position: relative;top: 25px;">
            <p>设备名称:<span style="color: #999;">{{ item.name || '-' }}</span></p>
            <p>设备编号:<span style="color: #999;">{{ item.num || '-' }}</span></p>
            <p>资产编号:<span style="color: #999;">{{ item.assetNum || '-' }}</span></p>
            <p>存放位置:<span style="color: #999;">{{ item.placeName || '-' }}</span></p>
          </div>
        </el-card>
      </div>
    </div>
    <!-- 修改设备基值 -->
    <template v-if="open">
      <AddbaseValue :title="title" :rowObj="rowObj"></AddbaseValue>
    </template>
  </div>
</template>

<script>
import { getequipmentlist, postqrCodeImglist, putequipmentlastPrintTime } from "@/api/equipment/equipment.js";
export default {
  name: "Label",
  // dicts: ['sys_notice_status', 'sys_notice_type'],
  components: {
    AddbaseValue: () => import('../form/addbaseValue.vue')
  },
  data () {
    return {
      // 遮罩层
      loading: true,
      loadingbtn: false,
      opendetailed: false,
      detailedObj: {},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      dictdatalist: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层表格数据
      noticeList: [],
      rowObj: {},
      // 弹出层标题
      title: "修改类别",
      // 是否显示弹出层
      open: false,
      selectionImg: [],
      browseList: [],
      selectionareaId: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'equipmentAccountId',//排序字段
        isAsc: 'desc'//倒叙
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        noticeTitle: [
          { required: true, message: "公告标题不能为空", trigger: "blur" }
        ],
        noticeType: [
          { required: true, message: "公告类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    async getList (value) {
      this.loading = true;
      let obj = value || {}
      let params = {
        ...this.queryParams,
        ...obj,
        'equipmentBaseVo.equipmentName': obj.equipmentName
      }
      //  let data = await getdatalist({ dictType: 'base_unit' })
      //  this.dictdatalist=data.data
      getequipmentlist(params).then(response => {
        this.noticeList = response.data && response.data.map((item, index) => {
          return {
            ...item,
            order: index + 1
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**
     * 打印方法
     */
    handlePrint () {
      if (this.selectionImg.length === 0) {
        return this.$message.error('请选择需打印的数据')
      }
      if (this.browseList.length === 0) return this.$message.error('请预览选择的数据')
      putequipmentlastPrintTime(this.selectionareaId.join(',')).then(() => { this.getList(); })
      var newWin = window.open(""); //新打开一个空窗口
      var imageToPrint = document.getElementById("printDiv"); //获取需要打印的内容
      newWin.document.write(imageToPrint.outerHTML); //将需要打印的内容添加进新的窗口
      const styleSheet = ``
      // newWin.document.addClass()
      newWin.document.head.innerHTML = styleSheet; //给打印的内容加上样式

      newWin.document.close(); //在IE浏览器中使用必须添加这一句

      newWin.focus(); //在IE浏览器中使用必须添加这一句

      setTimeout(function () {
        newWin.print(); //打印

        newWin.close(); //关闭窗口

      }, 100);

    },
    // 打开详细弹窗
    handledetailed (row) {
      this.opendetailed = true
      this.detailedObj = row
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.rowObj = row
      this.open = true
    },
    PreviewView () {//预览
      if (this.selectionImg.length === 0) {
        return this.$message.error('请选择需预览的数据')
      }
      this.loadingbtn = true
      let params = [
        ...this.selectionImg
      ]
      postqrCodeImglist(params).then(res => {
        this.browseList = []
        this.browseList = res.data
        this.loadingbtn = false
      }).catch(err => {
        this.loadingbtn = false
        this.$message.error(err)
      })
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.selectionareaId = selection.map(item => item.equipmentAccountId)
      console.log(selection);
      this.selectionImg = selection.map(item => {
        return {
          num: item.equipmentNum,//设备编号
          name: item.equipmentBaseVo.equipmentName,//设备名称
          assetNum: item.assetNum,//资产编号
          placeName: item.placeName//位置名称
        }
      })
      // this.single = selection.length != 1
      // this.multiple = !selection.length
    },
    /** 启用禁用按钮操作 */
    // handleDelete (row) {
    //   const noticeIds = row.typeName || '无名称'
    //   this.$confirm(`是否确认${row.status === '0' ? `禁用` : `启用`}类别名称为${noticeIds}的数据？`, '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     this.loading = true;
    //     let params = {
    //       typeId: row.typeId,
    //       status: row.status === '0' ? 1 : 0
    //     }
    //     putequipmentTypeChangeStatus(params).then(res => {
    //       if (res.code === 200) {
    //         this.loading = false;
    //         this.$message.success(res.msg)
    //         this.getList()
    //       }
    //     })
    //   }).catch(() => { this.loading = false; });
    // }
  },
  filters: {
  }
};
</script>
<style lang="scss" scoped>
.red {
  color: red;
}

.flex_label {
  // display: flex;
  // float: left;
  width: 100%;

  .label_div:nth-child(1) {
    // flex: 5;
    float: left;
    width: 55%;
  }

  .label_div:nth-child(2) {
    // flex: 5;
    width: 43%;
    float: left;
    // box-sizing: border-box;
    margin-left: 20px;
  }
}

.ImgCode_flex {
  margin-top: 10px;
  font-size: 14px;

  ::v-deep .el-card__body {
    display: flex;
    padding: 15px;

    .div_flex:nth-child(1) {
      flex: 1;

      img {
        width: 180px;
        height: 180px;
      }
    }

    .div_flex:nth-child(2) {
      flex: 2;
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      position: relative;
      top: 0 !important;

      span {
        color: #999;
      }
    }
  }
}
</style>
